﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

/*
 * Author:Conis
 * Date:
 * Description:SQL操作的工厂类
 */

namespace Conis.Squirrel.SqlBuilder
{
    public abstract class BaseSqlFactory
    {
        /// <summary>
        /// 事务
        /// </summary>
        protected IDbTransaction Transaction { get; set; }
        /// <summary>
        /// 连接对象
        /// </summary>
        protected IDbConnection Connection { get; set; }
        /// <summary>
        /// 连接字符串
        /// </summary>
        protected string ConnectionString { get; set; }

        #region 抽象方法
        /// <summary>
        /// 获取执行SQL语句的实现类
        /// </summary>
        /// <returns></returns>
        protected abstract BaseExecuteSql GetExecuteSql();
        /// <summary>
        /// 获取连接对象
        /// </summary>
        /// <returns></returns>
        protected abstract IDbConnection GetConnection();
        /// <summary>
        /// 获取条件的实现类
        /// </summary>
        /// <returns></returns>
        protected abstract BaseCondition GetCondition();
        /// <summary>
        /// 获取排序的实现类
        /// </summary>
        /// <returns></returns>
        protected abstract BaseOrderBy GetOrderBy();
        /// <summary>
        /// 获取数据访问类的实现类
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        protected abstract BaseDataAccess GetDataAccess(string tableName, string primaryKey);
        #endregion

        /// <summary>
        /// SQL操作的工厂类
        /// </summary>
        /// <param name="connectionString">连接字符串</param>
        public BaseSqlFactory(string connectionString)
        {
            this.ConnectionString = connectionString;
        }


        /// <summary>
        /// 返回一个条件类
        /// </summary>
        public BaseCondition Condition
        {
            get
            {
                return this.GetCondition();
            }
        }

        /// <summary>
        /// 返回一个查询类
        /// </summary>
        public BaseOrderBy OrderBy
        {
            get
            {
                return this.GetOrderBy();
            }
        }

        /// <summary>
        /// 返回一个数据访问类
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="primaryKey"></param>
        /// <returns></returns>
        public BaseDataAccess DataAccess(string tableName, string primaryKey)
        {
            return this.GetDataAccess(tableName, primaryKey);
        }

        /// <summary>
        /// 返回一个执行SQL类
        /// </summary>
        public BaseExecuteSql ExecuteSql
        {
            get
            {
                //判断连接是否已经存在
                if (this.Connection == null)
                {
                    this.Connection = this.GetConnection();
                }
                //判断连接是否关闭
                if (this.Connection.State == ConnectionState.Closed)
                {
                    this.Connection.Open();
                }
                return this.GetExecuteSql();
            }
        }

        /// <summary>
        /// 启用一个事务
        /// </summary>
        public void BeginTransaction()
        {
            if (this.Transaction == null)
            {
                if (this.Connection.State == System.Data.ConnectionState.Closed)
                {
                    this.Connection.Open();
                }
                this.Transaction = this.Connection.BeginTransaction();
            }
        }

        /// <summary>
        /// 提交事务
        /// </summary>
        public void CommitTransaction()
        {
            if (this.Transaction != null)
            {
                this.Transaction.Commit();
                this.Transaction.Dispose();
                this.Transaction = null;
                this.Connection.Close();
            }
        }

        /// <summary>
        /// 事务进行回滚
        /// </summary>
        public void RollbackTransaction()
        {
            if (this.Transaction != null)
            {
                this.Transaction.Rollback();
                this.Transaction.Dispose();
                this.Transaction = null;
                this.Connection.Close();
            }
        }
    }
}
